package me.dz.dreamcleaner.command;

import me.dz.dreamcleaner.listener.ChatEvent;
import me.dz.dreamcleaner.manager.WorldManager;
import me.dz.dreamcleaner.utils.ClearUtils;
import me.dz.dreamcleaner.utils.TrashUtils;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.HashMap;

import static me.dz.dreamcleaner.DreamCleaner.*;
import static me.dz.dreamcleaner.listener.RightEvent.bMap;
import static me.dz.dreamcleaner.manager.FlexibleFormula.mathematical;
import static me.dz.dreamcleaner.utils.MsgUtils.*;

public class ACommand extends ICommand{
    @ECommand(id = 0,order = "open",player = true,desc = "&a打开公共垃圾桶")
    public void open(CommandSender sender,String label,String[] args){
        if (trashEnable){
            if (sender.hasPermission(trashPermission)){
                Player player = (Player) sender;
                TrashUtils.open(player,0);
            }else {
                chat(sender,"&4你并没有使用公共垃圾桶的权限！");
            }
        }else {
            chat(sender,"&4这个服务器并没有开启公共垃圾桶！");
        }
    }

    @ECommand(id = 11,order = "reload",op = true,desc = "&a重载配置文件")
    public void reload(CommandSender sender,String label,String[] args){
        pl.loadConfig();
        chat(sender, "&e配置文件重载完毕");
    }

    @ECommand(id = 12,order = "item",op = true,desc = "&b[是否异步] &a进行一次垃圾清理")
    public void item(CommandSender sender,String label,String[] args){
        boolean async = itemAsync;
        if (args.length > 1){
            try {
                async = Boolean.parseBoolean(args[1]);
            }catch (Exception ignored){

            }
        }
        HashMap<World, ArrayList<Entity>> map = WorldManager.getEntities(itemBlackWorld,itemCheck);
        if (async){
            new BukkitRunnable(){
                @Override
                public void run() {
                    ClearUtils.runItem(map);
                }
            }.runTaskAsynchronously(pl);
            chat(sender,"&e成功进行一次异步垃圾清理！");
        }else {
            ClearUtils.runItem(map);
            chat(sender,"&e成功进行一次普通垃圾清理！");
        }
    }

    @ECommand(id = 13,order = "mobs",op = true,desc = "&b[是否异步] &a进行一次生物清理")
    public void mobs(CommandSender sender,String label,String[] args){
        boolean async = mobsAsync;
        if (args.length > 1) {
            try {
                async = Boolean.parseBoolean(args[1]);
            } catch (Exception ignored) {

            }
        }
        HashMap<World, ArrayList<Entity>> map = WorldManager.getEntities(mobsBlackWorld,mobsCheck);
        if (async) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    ClearUtils.runMobs(map, true);
                }
            }.runTaskAsynchronously(pl);
            chat(sender, "&e成功进行一次异步生物清理！");
        } else {
            ClearUtils.runMobs(map, true);
            chat(sender, "&e成功进行一次普通生物清理！");
        }
    }

    @ECommand(id = 14,order = "mobsAmount",op = true,desc = "&a测试生物清理的灵活公式")
    public void mobsAmount(CommandSender sender,String label,String[] args){
        String online = WorldManager.getOnline();
        String players = WorldManager.getPlayers();
        String formula = mobsAmount
                .replace("<online>",online)
                .replace("<players>",players);
        int amount = (int) mathematical(formula);
        chat(sender,"&e所有在线数量:&b " + online);
        chat(sender,"&e在线玩家数量:&b " + players);
        chat(sender,"&e灵活公式:&b " + mobsAmount);
        chat(sender,"&e灵活公式分析:&b " + formula);
        chat(sender,"&e计算结果:&b " + amount);
    }

    @ECommand(id = 15,order = "trash",op = true,desc = "&a清空公共垃圾桶")
    public void trash(CommandSender sender,String label,String[] args){
        TrashUtils.reset(false);
        chat(sender,"&e成功清空垃圾桶所有物品！");
    }

    @ECommand(id = 16,order = "check",op = true,player = true,desc = "&a开启或关闭检测实体类型")
    public void check(CommandSender sender,String label,String[] args){
        Player player = (Player) sender;
        boolean b = !bMap.getOrDefault(player.getName(), false);
        if (b) {
            chat(sender, "&e恭喜你开启了检测模式,请右键实体进行查询！");
        } else {
            chat(sender, "&e你已经成功关闭检测模式,放轻松！");
        }
        bMap.put(player.getName(), b);
    }

    @ECommand(id = 17,order = "type",op = true,player = true,desc = "&a获得手上物品材质名称")
    public void type(CommandSender sender,String label,String[] args){
        Player player = (Player) sender;
        ItemStack itemStack = player.getItemInHand();
        if (itemStack.getType() != Material.AIR) {
            chat(sender, "&e手上物品材质:&b " + itemStack.getType().toString());
        } else {
            chat(sender, "&4请手持物品执行该命令！");
        }
    }

    @ECommand(id = 18,order = "gc",op = true,desc = "&a进行一次运存清理")
    public void gc(CommandSender sender,String label,String[] args){
        Runtime runtime = Runtime.getRuntime();
        long a = runtime.totalMemory() - runtime.freeMemory();
        runtime.gc();
        runtime.runFinalization();
        long b = runtime.totalMemory() - runtime.freeMemory();
        if (a > b){
            chat(sender,"&e本次成功清理了&b " + ((a-b)/1024/1024) + "MB &e的运存");
        }else {
            chat(sender,"&e清理太过频繁,没有达到任何运存清理效果！");
        }
    }

    @ECommand(id = 19,order = "cache",op = true,desc = "&a进行一次聊天缓存清理")
    public void cache(CommandSender sender,String label,String[] args){
        ChatEvent.clear();
        chat(sender,"&e成功清理了聊天缓存！");
    }
}
